package com.itheima.edu.info.manger.dao;

import com.itheima.edu.info.manger.domain.Teacher;

public class TeacherDao {
    private static Teacher[] teachers=new Teacher[5];
    static {
        Teacher stu1=new Teacher("heima111","张老师","34","1899-11-11");
        Teacher stu2=new Teacher("heima222","李老师","36","1898-11-11");
        teachers[0]=stu1;
        teachers[2]=stu2;
    }
    public static boolean addTeacher(Teacher teach) {
      int index=-1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher==null){
                index=i;
                break;
            }
        }
        if (index==-1){
            return false;
        }else{
            teachers[index]=teach;
            return true;
        }
    }

    public static Teacher[] getAllTeacher() {
        return teachers;
    }

    public void deteleTeacherById(String did) {
        int index = getIndex(did);
        teachers[index]=null;
    }

    private int getIndex(String did) {
        int index=-1;
        for (int i = 0; i < teachers.length; i++) {
          Teacher teacher= teachers[i];
          if (teacher!=null&&teacher.getId().equals(did)){
              index=i;
              break;
          }
        }
        return index;
    }

    public void updateTeacher(Teacher newTeacher,String uid) {
        int index = getIndex(uid);
        teachers[index]=newTeacher;
    }
}
